package servlets1;

import beans1.Adminbean;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import service1.AdminService;

import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
@WebServlet("/Main_Admin")
public class Main_Admin  extends HttpServlet {
    @SneakyThrows
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action=request.getParameter("action");
        System.out.println("in Main.java,the action is "+action);
        if("delAdmin".equals(action))
        {
            System.out.println("i am in delAdmin");
            delAdmin(request, response);
        }

        else if("editAdmin".equals(action)) {
            System.out.println("i am in editAdmin");
            try {
				editAdmin(request, response);
			} catch (ParseException | IOException | SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            System.out.println("i am out editAdmin");
        } else if ("insertAdmin".equals(action)) {
            System.out.println("i am in insertAdmin");
            try {
                insertAdmin(request,response);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        } else
        {
            System.out.println("i am in showlist");
            showlist(request, response);
        }
    }

    private void insertAdmin(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException, ParseException {
        AdminService adminService=new AdminService();
        String account = request.getParameter("account");
        String password = request.getParameter("password");
        String photo = request.getParameter("photo");
        Adminbean str=new Adminbean(account, password,photo);
        adminService.insertAdmin(str);
        response.sendRedirect(request.getContextPath()+"/Main_Admin");
    }

    private void editAdmin(HttpServletRequest request, HttpServletResponse response) throws ParseException, IOException, SQLException {
        AdminService adminService=new AdminService();
        String account = request.getParameter("account");
        String password = request.getParameter("password");
        String photo = request.getParameter("photo");
        Adminbean str=new Adminbean(account,password,photo);
        adminService.editAdmin(str);
        response.sendRedirect(request.getContextPath()+"/Main_Admin");
    }

    private void delAdmin(HttpServletRequest request, HttpServletResponse response) throws IOException {
        AdminService adminService=new AdminService();
        Adminbean str=new Adminbean();
        str.setAccount(request.getParameter("account"));
        int result=adminService.delAdmin(str);
        if(result==1) {
            System.out.println("success");
        }
        else {
            System.out.println("false");
        }
        response.sendRedirect(request.getContextPath()+"/Main_Admin");
    }

    private void showlist(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        AdminService adminService=new AdminService();
        ArrayList<Adminbean> Adminbeans = adminService.browse();
        request.setAttribute("list", Adminbeans);
        request.getRequestDispatcher("admin/adminManage.jsp").forward(request,response);
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
